package com.itheima.behavior.controller.v1;

import com.itheima.behavior.service.RankingService;
import com.itheima.model.behavior.dtos.RankingQueryDto;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.utils.thread.MpThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 排行榜控制器
 */
@Api(tags = "排行榜API")
@RestController
@RequestMapping("/api/v1/ranking")
@Slf4j
public class RankingController {

    @Autowired
    private RankingService rankingService;

    // ========== 实时排行榜（Kafka Streams 计算）==========

    /**
     * 获取实时热度排行榜
     * 反映当前正在发生的热点，适合发现新热门内容
     */
    @ApiOperation("获取实时热度排行榜")
    @GetMapping("/book/realtime/hot")
    public ResponseResult getRealtimeHotRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "20") Integer limit) {
        return rankingService.getRealtimeHotRanking(limit);
    }

    // ========== 综合排行榜（定时任务计算）==========

    /**
     * 获取图书综合热度排行榜
     * 基于历史累积数据+时间衰减，适合发现经典热门内容
     */
    @ApiOperation("获取图书综合热度排行榜")
    @GetMapping("/book/hot")
    public ResponseResult getBookHotRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "50") Integer limit,
            @ApiParam("时间范围:today/week/month/all") @RequestParam(defaultValue = "all") String timeRange) {
        return rankingService.getBookHotRanking(limit, timeRange);
    }

    /**
     * 获取图书阅读排行榜
     */
    @ApiOperation("获取图书阅读排行榜")
    @GetMapping("/book/read")
    public ResponseResult getBookReadRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "50") Integer limit,
            @ApiParam("时间范围:today/week/month/all") @RequestParam(defaultValue = "all") String timeRange) {
        return rankingService.getBookReadRanking(limit, timeRange);
    }

    /**
     * 获取图书收藏排行榜
     */
    @ApiOperation("获取图书收藏排行榜")
    @GetMapping("/book/collect")
    public ResponseResult getBookCollectRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "50") Integer limit,
            @ApiParam("时间范围:today/week/month/all") @RequestParam(defaultValue = "all") String timeRange) {
        return rankingService.getBookCollectRanking(limit, timeRange);
    }

    /**
     * 获取图书点赞排行榜
     */
    @ApiOperation("获取图书点赞排行榜")
    @GetMapping("/book/like")
    public ResponseResult getBookLikeRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "50") Integer limit,
            @ApiParam("时间范围:today/week/month/all") @RequestParam(defaultValue = "all") String timeRange) {
        return rankingService.getBookLikeRanking(limit, timeRange);
    }

    /**
     * 获取用户阅读时长排行榜
     */
    @ApiOperation("获取用户阅读时长排行榜")
    @GetMapping("/user/reading-time")
    public ResponseResult getUserReadingTimeRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "100") Integer limit,
            @ApiParam("时间范围:today/week/month/all") @RequestParam(defaultValue = "all") String timeRange) {
        return rankingService.getUserReadingTimeRanking(limit, timeRange);
    }

    /**
     * 获取用户活跃度排行榜
     */
    @ApiOperation("获取用户活跃度排行榜")
    @GetMapping("/user/active")
    public ResponseResult getUserActiveRanking(
            @ApiParam("返回数量") @RequestParam(defaultValue = "100") Integer limit,
            @ApiParam("时间范围:today/week/month/all") @RequestParam(defaultValue = "all") String timeRange) {
        return rankingService.getUserActiveRanking(limit, timeRange);
    }

    /**
     * 通用排行榜查询
     */
    @ApiOperation("通用排行榜查询")
    @PostMapping("/query")
    public ResponseResult getRanking(@RequestBody RankingQueryDto dto) {
        return rankingService.getRanking(dto);
    }

    /**
     * 获取当前用户在排行榜中的排名
     */
    @ApiOperation("获取当前用户在排行榜中的排名")
    @GetMapping("/user/my-rank")
    public ResponseResult getMyRank(
            @ApiParam("排行榜类型") @RequestParam String rankingType) {
        Integer userId = MpThreadLocalUtil.getUser().getId();
        return rankingService.getUserRank(userId, rankingType);
    }

    /**
     * 获取指定用户在排行榜中的排名
     */
    @ApiOperation("获取指定用户在排行榜中的排名")
    @GetMapping("/user/{userId}/rank")
    public ResponseResult getUserRank(
            @ApiParam("用户ID") @PathVariable Integer userId,
            @ApiParam("排行榜类型") @RequestParam String rankingType) {
        return rankingService.getUserRank(userId, rankingType);
    }

    /**
     * 获取图书在排行榜中的排名
     */
    @ApiOperation("获取图书在排行榜中的排名")
    @GetMapping("/book/{bookId}/rank")
    public ResponseResult getBookRank(
            @ApiParam("图书ID") @PathVariable Integer bookId,
            @ApiParam("排行榜类型") @RequestParam String rankingType) {
        return rankingService.getBookRank(bookId, rankingType);
    }

    /**
     * 获取多个排行榜（首页展示）
     */
    @ApiOperation("获取多个排行榜（首页展示）")
    @GetMapping("/home")
    public ResponseResult getHomeRankings() {
        Map<String, Object> result = new HashMap<>();
        
        // 实时热搜榜 Top 10（正在流行）
        ResponseResult realtimeHotRanking = rankingService.getRealtimeHotRanking(10);
        result.put("realtimeHotRanking", realtimeHotRanking.getData());
        
        // 综合热度榜 Top 10（经典热门）
        ResponseResult hotRanking = rankingService.getBookHotRanking(10, "all");
        result.put("hotRanking", hotRanking.getData());
        
        // 图书阅读榜 Top 10
        ResponseResult readRanking = rankingService.getBookReadRanking(10, "all");
        result.put("readRanking", readRanking.getData());
        
        // 图书收藏榜 Top 10
        ResponseResult collectRanking = rankingService.getBookCollectRanking(10, "all");
        result.put("collectRanking", collectRanking.getData());
        
        return ResponseResult.okResult(result);
    }

    // ========== 笔记排行榜 ==========

    /**
     * 获取实时热门笔记排行榜
     */
    @ApiOperation("获取实时热门笔记排行榜")
    @GetMapping("/note/realtime/hot")
    public ResponseResult getRealtimeHotNotes(
            @ApiParam("返回数量") @RequestParam(defaultValue = "20") Integer limit) {
        return rankingService.getRealtimeHotNotes(limit);
    }

    /**
     * 获取综合热门笔记排行榜
     */
    @ApiOperation("获取综合热门笔记排行榜")
    @GetMapping("/note/hot")
    public ResponseResult getHotNotes(
            @ApiParam("返回数量") @RequestParam(defaultValue = "50") Integer limit) {
        return rankingService.getHotNotes(limit);
    }

    /**
     * 获取指定图书的热门笔记
     */
    @ApiOperation("获取指定图书的热门笔记")
    @GetMapping("/book/{bookId}/hot-notes")
    public ResponseResult getBookHotNotes(
            @ApiParam("图书ID") @PathVariable Integer bookId,
            @ApiParam("返回数量") @RequestParam(defaultValue = "10") Integer limit) {
        return rankingService.getBookHotNotes(bookId, limit);
    }

    // ========== 书评排行榜 ==========

    /**
     * 获取实时热门书评排行榜
     */
    @ApiOperation("获取实时热门书评排行榜")
    @GetMapping("/review/realtime/hot")
    public ResponseResult getRealtimeHotReviews(
            @ApiParam("返回数量") @RequestParam(defaultValue = "20") Integer limit) {
        return rankingService.getRealtimeHotReviews(limit);
    }

    /**
     * 获取综合热门书评排行榜
     */
    @ApiOperation("获取综合热门书评排行榜")
    @GetMapping("/review/hot")
    public ResponseResult getHotReviews(
            @ApiParam("返回数量") @RequestParam(defaultValue = "50") Integer limit) {
        return rankingService.getHotReviews(limit);
    }

    /**
     * 获取指定图书的热门书评
     */
    @ApiOperation("获取指定图书的热门书评")
    @GetMapping("/book/{bookId}/hot-reviews")
    public ResponseResult getBookHotReviews(
            @ApiParam("图书ID") @PathVariable Integer bookId,
            @ApiParam("返回数量") @RequestParam(defaultValue = "10") Integer limit) {
        return rankingService.getBookHotReviews(bookId, limit);
    }
}

